package com.cx.bank.handler;

import com.cx.bank.dao.BankDaoInterface;
import com.cx.bank.manager.IManager;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @author jxau_scf
 * @date 2021/8/4 - 11:10
 */
public class MyHandler {
    private Object target;

    public Object getTarget() {
        return target;
    }

    public void setTarget(Object target) {
        this.target = target;
    }

    public Object getProxy(){
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
//        Class[] classes = {BankDaoInterface.class};
        //使用lambada表达式
        InvocationHandler handler =(proxy, method, args) -> {
            System.out.println("代理前=============");
            Object result = method.invoke(this.target, args);
            //以下这两classLoader对象都一样
//            System.out.println("Thread.currentThread().getContextClassLoader():"+classLoader);
//            System.out.println("target.getClass().getClassLoader():"+target.getClass().getClassLoader());
            System.out.println("这里是代理后===============");
            return result;
        };
//        return Proxy.newProxyInstance(classLoader, target.getClass().getInterfaces(), handler);
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), handler);
    }
}
